cargo new: Don't create a git repo in subdirectory
authorPaul Woolcock <paul@woolcock.us>
Thu, 23 Oct 2014 12:30:36 +0000 (08:30 -0400)
committerPaul Woolcock <paul@woolcock.us>
Fri, 24 Oct 2014 18:34:11 +0000 (14:34 -0400)
Running `cargo new` to create a subpackage shouldn't create another
git repository if one exists in a parent directory.

Closes #664

Cargo.lock
src/cargo/ops/cargo_new.rs
src/cargo/util/vcs.rs
tests/test_cargo_new.rs

index 4dd9d66df1a89d24cdf7ebd6576bf97e3f8fef3b..df26dcd62911164193fc75957b4ca8d1b3d38ad6 100644 (file)
@@ -5,7 +5,7 @@ dependencies = [
  "curl 0.0.1 (git+https://github.com/alexcrichton/curl-rust?ref=bundle#36b015de91daf6310227cec04ef30acf5929dfb6)",
  "docopt 0.6.4 (git+https://github.com/docopt/docopt.rs#4544a9f422b115c2ffef4ee9baf27ceb07c34602)",
  "flate2 0.0.1 (git+https://github.com/alexcrichton/flate2-rs#68971ae77a523c7ec3f19b4bcd195f76291ea390)",
- "git2 0.0.1 (git+https://github.com/alexcrichton/git2-rs#c01b0b279470552c48cf7f902ae5baf132df0a6d)",
+ "git2 0.0.1 (git+https://github.com/alexcrichton/git2-rs#7d7fba10893590793ae88c8fc6ab2aeffcb8f10b)",
  "glob 0.0.1 (git+https://github.com/rust-lang/glob#469a6bc1a0fc289ab220170e691cffbc01dcf1e6)",
  "hamcrest 0.1.0 (git+https://github.com/carllerche/hamcrest-rust.git#7d46e76514ac606530dfb0e93270fffcf64ca76d)",
  "semver 0.1.0 (git+https://github.com/rust-lang/semver#9bb8265ea6cf01eddfa7dc5ec9334883443e9fc7)",
@@ -47,9 +47,9 @@ source = "git+https://github.com/alexcrichton/flate2-rs#68971ae77a523c7ec3f19b4b
 [[package]]
 name = "git2"
 version = "0.0.1"
-source = "git+https://github.com/alexcrichton/git2-rs#c01b0b279470552c48cf7f902ae5baf132df0a6d"
+source = "git+https://github.com/alexcrichton/git2-rs#7d7fba10893590793ae88c8fc6ab2aeffcb8f10b"
 dependencies = [
- "libgit2 0.0.1 (git+https://github.com/alexcrichton/git2-rs#c01b0b279470552c48cf7f902ae5baf132df0a6d)",
+ "libgit2 0.0.1 (git+https://github.com/alexcrichton/git2-rs#7d7fba10893590793ae88c8fc6ab2aeffcb8f10b)",
  "url 0.1.0 (git+https://github.com/servo/rust-url#7f1991d847fb8cf8648def2ff121bae90b89131f)",
 ]
 
@@ -66,7 +66,7 @@ source = "git+https://github.com/carllerche/hamcrest-rust.git#7d46e76514ac606530
 [[package]]
 name = "libgit2"
 version = "0.0.1"
-source = "git+https://github.com/alexcrichton/git2-rs#c01b0b279470552c48cf7f902ae5baf132df0a6d"
+source = "git+https://github.com/alexcrichton/git2-rs#7d7fba10893590793ae88c8fc6ab2aeffcb8f10b"
 dependencies = [
  "libssh2-static-sys 0.0.1 (git+https://github.com/alexcrichton/libssh2-static-sys#80e71a3021618eb05656c58fb7c5ef5f12bc747f)",
  "link-config 0.0.1 (git+https://github.com/alexcrichton/link-config#0202cc8aa74a7b0bdbaef4eef368d0bc80f63691)",
index 856603bfec8c52d5588e0f00daad5b2bbd71f9ac..d5c0d18ef895fd8958336eb5eddb353c1f321f2e 100644 (file)
@@ -41,9 +41,15 @@ pub fn new(opts: NewOptions, _shell: &mut MultiShell) -> CargoResult<()> {
     })
 }
 
+fn existing_git_repo(path: &Path) -> bool {
+    GitRepo::discover(path).is_ok()
+}
+
 fn mk(path: &Path, name: &str, opts: &NewOptions) -> CargoResult<()> {
     let cfg = try!(global_config());
     let mut ignore = "/target\n".to_string();
+    let no_git = !opts.git && (opts.no_git || cfg.git == Some(false));
+    let in_existing_git_repo = existing_git_repo(&path.dir_path());
     if !opts.bin {
         ignore.push_str("/Cargo.lock\n");
     }
@@ -51,7 +57,7 @@ fn mk(path: &Path, name: &str, opts: &NewOptions) -> CargoResult<()> {
     if opts.hg {
         try!(HgRepo::init(path));
         try!(File::create(&path.join(".hgignore")).write(ignore.as_bytes()));
-    } else if !opts.git && (opts.no_git || cfg.git == Some(false)) {
+    } else if no_git || in_existing_git_repo {
         try!(fs::mkdir(path, io::USER_RWX));
     } else {
         try!(GitRepo::init(path));
index 04b2fb376d6d103e8b37f29ac624dffc53fcfcfc..b4f7ce290c2509f2cff9a1624088e6d54cfd6a4e 100644 (file)
@@ -10,6 +10,9 @@ impl GitRepo {
         try!(git2::Repository::init(path));
         return Ok(GitRepo)
     }
+    pub fn discover(path: &Path) -> Result<git2::Repository,git2::Error> {
+        git2::Repository::discover(path)
+    }
 }
 
 impl HgRepo {
index cc218483843ba11234ada7a2d4e65189213bcd80..7539a0475c0483ac148d46ceecb38ea01e770815 100644 (file)
@@ -53,17 +53,18 @@ test!(simple_bin {
 })
 
 test!(simple_git {
+    let td = TempDir::new("cargo").unwrap();
     os::setenv("USER", "foo");
-    assert_that(cargo_process("new").arg("foo"),
+    assert_that(cargo_process("new").arg("foo").cwd(td.path().clone()),
                 execs().with_status(0));
 
-    assert_that(&paths::root().join("foo"), existing_dir());
-    assert_that(&paths::root().join("foo/Cargo.toml"), existing_file());
-    assert_that(&paths::root().join("foo/src/lib.rs"), existing_file());
-    assert_that(&paths::root().join("foo/.git"), existing_dir());
-    assert_that(&paths::root().join("foo/.gitignore"), existing_file());
+    assert_that(td.path(), existing_dir());
+    assert_that(&td.path().join("foo/Cargo.toml"), existing_file());
+    assert_that(&td.path().join("foo/src/lib.rs"), existing_file());
+    assert_that(&td.path().join("foo/.git"), existing_dir());
+    assert_that(&td.path().join("foo/.gitignore"), existing_file());
 
-    assert_that(cargo_process("build").cwd(paths::root().join("foo")),
+    assert_that(cargo_process("build").cwd(td.path().clone().join("foo")),
                 execs().with_status(0));
 })
 
@@ -157,6 +158,7 @@ test!(author_prefers_cargo {
 
 test!(git_prefers_command_line {
     let root = paths::root();
+    let td = TempDir::new("cargo").unwrap();
     fs::mkdir(&root.join(".cargo"), USER_RWX).assert();
     File::create(&root.join(".cargo/config")).write_str(r#"
         [cargo-new]
@@ -165,8 +167,23 @@ test!(git_prefers_command_line {
         email = "bar"
     "#).assert();
 
-    assert_that(cargo_process("new").arg("foo").arg("--git")
+    assert_that(cargo_process("new").arg("foo").arg("--git").cwd(td.path().clone())
                                     .env("USER", Some("foo")),
                 execs().with_status(0));
-    assert!(root.join("foo/.gitignore").exists());
+    assert!(td.path().join("foo/.gitignore").exists());
+})
+
+test!(subpackage_no_git {
+    os::setenv("USER", "foo");
+    assert_that(cargo_process("new").arg("foo"), execs().with_status(0));
+
+    let subpackage = paths::root().join("foo").join("components");
+    fs::mkdir(&subpackage, USER_RWX).assert();
+    assert_that(cargo_process("new").arg("foo/components/subcomponent"),
+                execs().with_status(0));
+
+    assert_that(&paths::root().join("foo/components/subcomponent/.git"),
+                 is_not(existing_file()));
+    assert_that(&paths::root().join("foo/components/subcomponent/.gitignore"),
+                 is_not(existing_file()));
 })